/*
 * MainWorkTask.cpp
 *
 *  Created on: Apr 26, 2017
 *      Author: zhangjf
 */

#include <Poco/Util/Application.h>

using namespace Poco::Util;

#include "Helper.h"
#include "DictionaryService.h"
#include "NetworkSubsystem.h"
#include "CustomNotification.h"
#include "MainWorkerTask.h"

using namespace util;
using namespace network;

namespace global
{

MainWorkerTask::MainWorkerTask()
: Task("MainWorkTask")
, _logger(Application::instance().logger().get("MainWorkTask"))
{
//	if (_pInstance != nullptr)
//		return;
//	_pInstance = this;
	// TODO Auto-generated constructor stub
	std::string fname = std::string(Application::instance().name()) + ".log";
	Helper::initLogger(logger(), fname);

	poco_information(logger(), __func__ + string(": ") + "ok");
}

MainWorkerTask::~MainWorkerTask()
{
	// TODO Auto-generated destructor stub
}

void MainWorkerTask::runTask()
{
	poco_information(logger(), __func__ + string(": ") + "start runing");

	DictionaryService::instance().initialize();
	NetworkSubsystem& network = Application::instance().getSubsystem<NetworkSubsystem>();
	network.start();

	while (!isCancelled())
	{
		CustomNotificationBase* pNotif = dynamic_cast<CustomNotificationBase*>(_nq.waitDequeueNotification());

		if (pNotif != nullptr)
		{
			poco_information(logger(), "Todo:: wait to compleate the dispatch notification....");
		}
	}

	network.stop();
}

void global::MainWorkerTask::enqueueNotification(Notification::Ptr pNotification)
{
	_nq.enqueueNotification(pNotification);
}

} /* namespace global */

